package com.steam.basic.service.impl;

import com.steam.basic.mapper.BaseMapper;
import com.steam.basic.query.BaseQuery;
import com.steam.basic.service.IBaseService;
import com.steam.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //1.查询总条数
        Integer totals = baseMapper.queryCount(query);
        //如果总数为0，没有必要去查询分页数据了 - 反正查询出来都是一个空集合
        if(totals==0){
            return new PageList<>();
        }

        //2.查询当前页的分页数据
        List<T> data = baseMapper.queryData(query);
        return new PageList<>(totals,data);
    }

    @Override
    public void patchRemove(List<Long> ids) {
        baseMapper.patchRemove(ids);
    }
}
